package com.wgu.modules.repository;

import com.wgu.entity.ChapterInfo;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @Author: w
 * @Date: 2019/7/4 17:36
 * @Version 1.0
 */
@Repository
public interface ChapterRepository extends JpaRepository<ChapterInfo,Long>{
    /**
     * 根据书名查询全部起点章节
     * @param bookName
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT * FROM  qd_chapter where name = ?1 ORDER BY id ASC ")
    List<Map<String,Object>> findAllQDChapterByBookName(String bookName);

    /**
     * 根据书Id及标题查询章节
     * @param bookId
     * @param title
     * @return
     */
    Optional<ChapterInfo> findByBookIdAndTitle(Long bookId,String title);

    /**
     * 根据书id查询章节id最大值
     * @param bookId
     * @return
     */
    @Query(nativeQuery = true,value = "SELECT max(id) FROM wgu_chapter_info where book_id = ?1")
    Long findMaxIdByBookId(Long bookId);

    /**
     * 根据书Id查询全部章节
     * @param bookId
     * @return
     */
    List<ChapterInfo> findByBookId(Long bookId);
}
